var erpCode = getQueryString("erpCode");
$(function () {
    $("#jqGrid").Grid({
        url: '../toolspartout/list?systemCode='+erpCode,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '申请人', name: 'applyUserName', index: 'APPLY_USER', width: 80},
            {label: '申请部门', name: 'useUnit', index: 'USE_UNIT', width: 80},
            {label: '工装名称', name: 'name', index: 'NAME', width: 80},
            {label: '系统编码', name: 'systemCode', index: 'SYSTEM_CODE', width: 80},
            {label: '出库数量', name: 'outNum', index: 'OUT_NUM', width: 80},
            {label: '出库时间', name: 'outDate', index: 'OUT_DATE', width: 80,
                formatter: function (v) {
                    return transDate(v)
                }
            },
            {label: '物料编码', name: 'partCode', index: 'PART_CODE', width: 80},
            {label: '实际出库量', name: 'relNum', index: 'REL_NUM', width: 80},
            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '单位', name: 'partUnit', index: 'PART_UNIT', width: 80},
            {label: '货位', name: 'cargoLocation', index: 'CARGO_LOCATION', width: 80},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {label: '出库仓库', name: 'outStore', index: 'OUT_STORE',hidden: true},
            ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsPartOut: {
            isDel: '',
            remark: '',
            relNum: '',
            outNum: '',
            cargoLocation: '',
            partUnit: '',
            spec: '',
            name: '',
            partCode: '',
            outStoreId: '',
            outStore: '',
            useUnit: '',
            outDate: '',
            outCode: '',
            id: '',
            checkStatus: '',
            checkTime: '',
            checkUser: '',
            systemCode: '',
            expectUseTime: '',
            applyTime: '',
            applyUser: '',
            partId: '',
            updateDate: '',
            updateUser: '',
            createUser: '',
            createDate: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ],
            outNum: [
                {required: true, message: '出库数量不能为空', trigger: 'blur'}
            ],
            systemCode: [
                {required: true, message: '系统编号不能为空', trigger: 'blur'}
            ],
            outCode: [
                {required: true, message: '出库单号不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            mapNo: '',
            useProcedure: '',
            beginTime: '',
            endTime: '',
        },
        toolsStockList: {},
        partName:'',
        storeId:'', //仓库ID
        storeName:'',//仓库名字
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsPartOut = {
                isDel: '1',
                remark: '',
                relNum: '',
                outNum: '',
                cargoLocation: '',
                partUnit: '',
                spec: '',
                name: '',
                partCode: '',
                outStoreId: '',
                outStore: '',
                useUnit: '',
                outDate: '',
                outCode: '',
                id: '',
                checkStatus: '',
                checkTime: '',
                checkUser: '',
                systemCode: '',
                expectUseTime: '',
                applyTime: '',
                applyUser: '',
                partId: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
            };
            var date = new Date();
            vm.toolsPartOut.outCode = 'OUT-' + date.getFullYear() + (date.getMonth() + 1) + (date.getDate()) + date.getHours() + date.getMinutes() + date.getSeconds()
            vm.getInfoByCode(erpCode)
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        getStorePos: function () {
            Ajax.request({
                url: "../toolsstorepos/queryAll",
                params: {"storeId": vm.outStoreId,"toolsCode":vm.toolsPartOut.partCode},
                async: true,
                successCallback: function (r) {
                    vm.optionList = r.list;
                }
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.toolsPartOut.id == (null || '') ? "../toolspartout/save" : "../toolspartout/update";

            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsPartOut),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolspartout/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getStoreHouse: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-institution"></i> 选择仓库',
                content: '../sys/storeHouseSelect.html?type=2',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getStoreHouse();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.toolsPartOut.outStoreId = res.id;
                        vm.toolsPartOut.outStore = res.name;
                        // vm.toolsPartIn.workshopName = res.name;
                        //获取位置列表
                        vm.getStorePos();
                    }
                }
            })
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolspartout/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsPartOut = r.toolsPartOut;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            let param = {};
            if(vm.q.name){
                param.name = vm.q.name;
            }
            if(vm.q.mapNo){
                param.mapNo = vm.q.mapNo;
            }
            if(vm.q.useProcedure){
                param.useProcedure = vm.q.useProcedure;
            }
            if(vm.q.beginTime){
                param.beginTime = gmt2DateStr(vm.q.beginTime);
            }
            if(vm.q.endTime){
                param.endTime = gmt2DateStr(vm.q.endTime);
            }

            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: param,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                mapNo: '',
                useProcedure: '',
                beginTime: '',
                endTime: '',
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getToolsStock: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择工装',
                content: '../tools/toolsStockSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSparePart();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                   //     console.log(res)
                        vm.toolsPartOut.systemCode = res.erpCode, //系统编码
                        vm.toolsPartOut.partCode = res.erpCode, //系统编码
                     //   vm.toolsPartOut.name = res.name;//名称
                        vm.toolsPartOut.spec = res.specs;//规格
                        vm.toolsPartOut.partUnit = res.unit;//单位
                        vm.toolsPartOut.outStore = res.workshopName;//出库仓库
                        vm.toolsPartOut.outStoreId = res.workshop;//出库仓库
                        vm.toolsPartOut.cargoLocation = res.cargoLocation;//货位
                        vm.toolsPartOut.partId = res.belongTools;//所属工装

                        //console.log(vm.toolsPartOut.outCode)
                    }
                }
            })
        },
      /*  review: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            //  console.log(rows)
            if (rows.checkStatus == "已审核") {
                alert('审核已通过,请勿重复审核!', function (index) {
                    vm.reload();
                });
                return;
            }
            vm.getInfo(ids);
            openWindow({
                title: "审核",
                area: ['550px', '300px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    Ajax.request({
                        url: '../toolspartout/review',
                        params: JSON.stringify(vm.toolsPartOut),
                        type: 'POST',
                        contentType: "application/json",
                        successCallback: function (result) {
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        }
                    });
                }
            });
        },*/
        getInfoByCode: function (erpCode) {
            Ajax.request({
                url: "../toolsstock/queryByCode/" + erpCode,
                async: true,
                successCallback: function (r) {
                   // console.log(r)
                    vm.toolsPartOut.systemCode = r.toolsStock.erpCode;//系统编码
                    vm.toolsPartOut.spareCode = r.toolsStock.erpCode;//系统编码
                    vm.toolsPartOut.partId = r.toolsStock.belongTools;//所属工装
                    vm.partName = r.toolsStock.toolsName;//所属工装
                    vm.toolsPartOut.name = r.toolsStock.name;//名称
                    vm.toolsPartOut.partUnit = r.toolsStock.unit;//单位
                    vm.toolsPartOut.partPrice = r.toolsStock.price;//单价
                    vm.toolsPartOut.spec = r.toolsStock.specs;//规格
                    vm.toolsPartOut.cargoLocation = r.toolsStock.cargoLocation;//货位
                    vm.toolsPartOut.outStore = r.toolsStock.workshopName;//入库仓库
                    vm.toolsPartOut.outStoreId = r.toolsStock.workshop;//入库仓库
                }
            });
        },
        goToolsBack: function(){

            var result = getSelectedRowData("#jqGrid");
            console.log(result.relNum)
            var id =result.id;
       /*     console.log(result)
            if (result.checkStatus!="已审核"){
                alert("此条出库记录不能进行退库操作!")
                return
            }*/
            if (id == null) {
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 工装退库',
                content: '../tools/toolspartback.html?outId='+id+'&relNum'+result.relNum
            })
        }
    }
});
function dateFtt(dateStr) {
    var date = new Date(dateStr);//当前时间
    var month = zeroFill(date.getMonth() + 1);//月
    var day = zeroFill(date.getDate());//日
    var hour = zeroFill(date.getHours());//时
    var minute = zeroFill(date.getMinutes());//分
    var second = zeroFill(date.getSeconds());//秒

    //当前时间
    var curTime = date.getFullYear() + "-" + month + "-" + day
        + " " + hour + ":" + minute + ":" + second;

    return curTime;
}